package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Author: Zheng
 * Date:   2024/2/24  11:28
 * Package：com.sky.service.impl
 * Description：
 * Version：1.0
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @param shoppingCartDTO
     * @description： 添加购物车
     * @date：2024/2/24 11:37
     * @return：
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //构造购物车对象，拷贝属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //添加购物车之前，先查询数据库中是否存在该商品
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> carts = shoppingCartMapper.listBy(shoppingCart);

        //如果商品存在，就更新商品数量+1，调用mapper执行update操作
        if (carts != null && !carts.isEmpty()) {

            ShoppingCart cart = carts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        } else {
            //反之，购物车商品不存在，补全属性，调用mapper执行新增方法，插入数据到shopping_cart表中
            //判断是添加菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {//是菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {//是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * @description： 查询购物车
     * @date：2024/2/24 17:10
     * @return：
     **/
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
    }

    /**
     * @description： 清空购物车
     * @date：2024/2/24 17:26
     * @return：
     **/
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * @param shoppingCartDTO
     * @description： 删除购物车中的一个商品
     * @date：2024/2/24 17:44
     * @return：
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> carts = shoppingCartMapper.listBy(shoppingCart);

        if (carts == null || carts.isEmpty()){
            throw  new DeletionNotAllowedException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        //如果购物车商品数据不为空
        ShoppingCart cart = carts.get(0);
        log.info("shooppingCart={},cart={}",shoppingCart,cart);

        //获取商品数量
        Integer number = cart.getNumber();
        //如果商品数量为1，直接从购物车删除商品
        if (number > 1) {
            //商品数量不为1，将商品数量-1，执行修改操作
            cart.setNumber(number - 1);
            shoppingCartMapper.update(cart);
        } else {
            shoppingCartMapper.deleteById(cart.getId());
        }

    }
}



